<?php

require_once "settings.inc";
require_once "lang.inc";

function formatColor($color) {
    # return "255:0:0";
    return hexdec(substr($color, 1, 2)) . ':' .
           hexdec(substr($color, 3, 2)) . ':' .
           hexdec(substr($color, 5, 2)); 
}

function formatDate($formatStr, $date) {
   $daysOfWeekArray = explode(',', LANG_WEEK_DAYS);
   return $daysOfWeekArray[date('w', $date)] . " " . date($formatStr, $date);
}

function readLocalFile($filename) {
    $fh = fopen($filename, 'r');
    if (false == $fh) {
        return false;
    }
    $content = "";         
    while($data = fread($fh, 1024)) {
        $content .= $data;
    }
    fclose($fh);
    return $content;
}

function tmplReplace($placeholder, $value, $template) {
    return str_replace("%%$placeholder%%", 
        isset($value) ? $value : "", $template);
}

# go over class reflection in order to substitute fields
function tmplReplaceObject($object, $template) {
    if (is_object($object)) {
        foreach (get_object_vars($object) as $name => $value) {
            $template = tmplReplace($name, $value, $template);
        }
    }
    return $template;
}

function finalizeTemplate($template) {
    $template = tmplReplace("HOME_URL", HOME_URL, $template);
    return preg_replace('|%%.*?%%|', '', $template);
}

function generateEpgLink($id, $name = null, $logo = null, $epgDate = null) {
    $link = HOME_URL . "showEpg.php?id=$id";
    if (isset($name)) {
        $link .= "&amp;name=" . urlencode($name);
    }
    if (isset($logo)) {
        $link .= "&amp;logo=" . urlencode($logo);
    }
    if (isset($epgDate)) {
        $link .= "&amp;epgDate=" . urlencode($epgDate);
    }
    return $link;
}

function drawCategoryPage($categories ) {
    $template = readLocalFile(TPL_CATEGORY_PAGE);
    if (false === $template) {
        return false;
    }
    $items = "";
    foreach ($categories as $category) {
        $items .= drawCategoryItem($category);
    }
    $template = tmplReplace("items", $items, $template);
    return finalizeTemplate($template);
}

function drawCategoryItem($category) {
    $template = readLocalFile(TPL_CATEGORY_ITEM);
    if (false === $template) {
        return false;
    }
    $template = tmplReplaceObject($category, $template);
    return finalizeTemplate($template);
}

function drawChannelPage($categories) {
    $template = readLocalFile(TPL_CHANNEL_PAGE);
    if (false === $template) {
        return false;
    }
    
    $items = "";
    $menuItems = "";
    foreach ($categories as $category) {
        foreach ($category->channels as $channel) {
            $items .= drawChannelItem($channel);
            $menuItems .= drawChannelItem($channel, TPL_CHANNEL_MENU);
        }
    }
    $template = tmplReplace("menuItems", $menuItems, $template);
    $template = tmplReplace("items", $items, $template);
    return finalizeTemplate($template);
}

function drawChannelItem($channel, $templateFileName = TPL_CHANNEL_ITEM) {
    $template = readLocalFile($templateFileName);
    if (false === $template) {
        return false;
    }

    $epgRef = generateEpgLink($channel->id, $channel->name, $channel->logo);
    $template = tmplReplace("epgRef", $epgRef, $template);
    $template = tmplReplaceObject($channel, $template);
    
    if (isset($channel->beginTime) && isset($channel->endTime)) {
        $time  = date('H:i', $channel->beginTime) . ' - ';
        $time .= date('H:i', $channel->endTime);
        $template = tmplReplace("humanTime", $time, $template);
    }

    # $videoUrl  = HOME_URL . "playChannel.php?alias=$channel->alias";
    $functions = new RtvFunctions();
    $videoUrl = $functions->getStreamUrl($channel->alias);
    
    $template = tmplReplace("videoUrl", $videoUrl, $template);
    return finalizeTemplate($template);
}

function drawEpgPage($channelId, $channelName, $channelLogo, $epgDate, $programs, $selectedIndex) {
    $template = readLocalFile(TPL_EPG_PAGE);
    if (false === $template) {
        return false;
    }

    $nowTime = NOW_TIME;
    if (date('d.m', $epgDate) != date('d.m', $nowTime)) {
        $epgDateLabel = "(" . LANG_EPG_FROM . " " . 
            formatDate('d.m', $epgDate) . ")";
        $template = tmplReplace("epgDate", $epgDateLabel, $template);
    }
    $nowTime = formatDate('d.m H:i', $nowTime);
    $prevEpgDay = generateEpgLink($channelId, $channelName, 
        $channelLogo, $epgDate - (24 * 60 * 60));
    $nextEpgDay = generateEpgLink($channelId, $channelName, 
        $channelLogo, $epgDate + (24 * 60 * 60));

    $template = tmplReplace("nowDate", $nowTime, $template);
    $template = tmplReplace("channelName", $channelName, $template);
    $template = tmplReplace("channelLogo", $channelLogo, $template);
    $template = tmplReplace("prevEpgDay", $prevEpgDay, $template);
    $template = tmplReplace("nextEpgDay", $nextEpgDay, $template);
    $template = tmplReplace("selectedIndex", $selectedIndex, $template);

    $index = 0;
    $items = "";
    foreach ($programs as $program) {
        $items .= drawEpgItem($program, $index == $selectedIndex ? 
            "current" : ($index < $selectedIndex ? "past" : "future"));
        $index++;
    }
    $template = tmplReplace("items", $items, $template);
    return finalizeTemplate($template);
}

function drawEpgItem($program, $timeSlot) {
    $template = readLocalFile(TPL_EPG_ITEM);
    if (false === $template) {
        return false;
    }
    $template = tmplReplaceObject($program, $template);
    $template = tmplReplace("timeSlot", $timeSlot, $template);
    return finalizeTemplate($template);
}

?>
